<?php
session_start();
  require_once('library/googlecart.php');
  require_once('library/googleitem.php');
  require_once('library/googleshipping.php');
  //require_once('library/googletax.php');
  require_once('includes/constants.php');
  require_once('includes/common.php');

   
   
  $merchant_id = GOOGLE_MERCHANT_ID;   
  $merchant_key = GOOGLE_MERCHANT_KEY;   
  $server_type = ENVIRONMENT;
  $currency = CURRENCY;
  $googleCart = new GoogleCart($merchant_id, $merchant_key, $server_type,  $currency);  
  
  $weight = 0;

  foreach ($_SESSION[CART] as $cartItem) {


  	$selectedGarmentID = $cartItem[ GARMENT ];
  	$selectedFabricID = $cartItem[ FABRIC ];
  	$selectedSizeID = $cartItem[ SIZE ];
   	log1("\nselectedGarmentID = $selectedGarmentID \n");
	log1( "selectedFabricID = $selectedFabricID \n");
	log1 ("selectedSizeID = $selectedSizeID \n"); 
  	$garment = $_SESSION[GARMENTS][$selectedGarmentID];
  	$fabric = $_SESSION[FABRICS][$selectedFabricID];
  	$size = $_SESSION[SIZES][$selectedSizeID];
  	$itemName = $garment['g_name'] . " in " . $fabric['f_name'];
  	$itemDescription = $garment['g_description'];
  	$itemPrice = $cartItem[ PRICE ];
  
  	$weight += $garment['g_est_fabric_required'] * $fabric['f_weight_per_m'];
  
  	$quantity = 1;
  
  	$googleItem = new GoogleItem($itemName, $itemDescription, $quantity, $itemPrice);
  	//$googleItem->SetMerchantItemId($selectedGarmentID . "-" . $selectedFabricID . "-" . $selectedSizeID);
	
	if (isset($_SESSION[CUSTOMER_ID])) {
		 
	 
  		$googleItem->SetMerchantPrivateItemData(new MerchantPrivateItemData(array (
	  	 
	  			's_size_id' => $_SESSION[ SIZES ][$selectedSizeID]['s_size_id'],
  	 
			"garment" => $selectedGarmentID,
			"fabric" => $selectedFabricID
  	)));		
	}
	else {
		echo "customer id not set   \n";
	  	$googleItem->SetMerchantPrivateItemData(new MerchantPrivateItemData(array (
 
	  			"s_name" => $_SESSION[ SIZES ][$selectedSizeID]["s_name"],
	  			"s_shoulder" => $_SESSION[ SIZES ][$selectedSizeID]["s_shoulder"],
	  			"s_chest" => $_SESSION[ SIZES ][$selectedSizeID]["s_chest"],
	  			"s_bust" => $_SESSION[ SIZES ][$selectedSizeID]["s_bust"],
	  			"s_waist" => $_SESSION[ SIZES ][$selectedSizeID]["s_waist"],
	  			"s_hips" => $_SESSION[ SIZES ][$selectedSizeID]["s_hips"],
	  			"s_back_waist" => $_SESSION[ SIZES ][$selectedSizeID]["s_back_waist"],
	  			"s_leg" => $_SESSION[ SIZES ][$selectedSizeID]["s_leg"],
	  			"s_height" => $_SESSION[ SIZES ][$selectedSizeID]["s_height"],
   
			"garment" => $selectedGarmentID,
			"fabric" => $selectedFabricID
  	)));		
	}

  	$googleCart->AddItem($googleItem);
  }

	if (isset($_SESSION[CUSTOMER_ID])) { 
		//order placed by logged in customer
		//send his ID along with the order so that he may be coupled with this order of he proceeds with the checkout
		 $googleCart->SetMerchantPrivateData(new MerchantPrivateData(array("customer-id" => $_SESSION[CUSTOMER_ID])));
	}
   
	

  $suitablePostalServices = getAvailablePostalServices($weight);
  foreach ($suitablePostalServices as $name => $price) {
  	$shipping = new GoogleFlatRateShipping($name, $price);
	//$Gfilter = new GoogleShippingFilters();
	//$Gfilter -> SetAllowUsPoBox(false);
	//$Gfilter -> SetAllowedWorldArea(false);
	//$Gfilter->SetAllowedCountryArea('UK');
	//$Gfilter->AddAllowedPostalArea("GB");
	//$shipping->AddShippingRestrictions($Gfilter);	
  	$googleCart->AddShipping($shipping);
  }

  $googleCart->SetEditCartUrl("http://localhost/");
  $googleCart->SetContinueShoppingUrl("http://localhost/");
  $googleCart->SetRequestBuyerPhone(true);

  list($status, $error) = $googleCart->CheckoutServer2Server();
  // if i reach this point, something was wrong
  echo "An error had ocurred: <br />HTTP Status: " . $status. ":";
  echo "<br />Error message:<br />";
  echo $error;
	
?>
